package com.wpp.websocket.handler;

import org.springframework.util.ConcurrentReferenceHashMap;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;
import org.springframework.web.socket.handler.TextWebSocketHandler;

import java.util.Map;

/**
 * @author thisisit
 * @class: WsHandler
 * @desc: TODO
 * @date: 2021/6/17 21:42
 */
public class WsHandler extends TextWebSocketHandler {
    Map<String, WebSocketSession> onlineWebSocketSession = new ConcurrentReferenceHashMap<>(100);

    @Override
    protected void handleTextMessage(WebSocketSession session, TextMessage message) throws Exception {
        Map<String, Object> attributes = session.getAttributes();
        System.out.println(attributes);
        String payload = message.getPayload();
        System.out.println(payload);
        TextMessage resp = new TextMessage("resp" + message.getPayload());
        session.sendMessage(resp);
    }

    @Override
    public void afterConnectionEstablished(WebSocketSession session) throws Exception {
        //进行保存处理
        System.out.println("保存会话");
        Object auth = session.getAttributes().get("auth");
        if (auth == null) {
            session.getAttributes().put("auth", "xxx");
            session.getAttributes().put("userId", "读取");
        }
        super.afterConnectionEstablished(session);
//        onlineWebSocketSession.put()
    }
}
